
$.ajaxSetup({
    complete:function(XMLHttpRequest,status){
    	var path = subUrl();
    	var str=XMLHttpRequest.responseText;
    	if(str.indexOf("SHIRO_EXPIRE")>0){
    		window.top.location.href = path+'/index.do';
    	}
    }
})

function bootstrapTableShiroExpire(res){
	var path = subUrl();
	if(res.httpCode == "SHIRO_EXPIRE"){
		window.top.location.href = path+'/index.do';
	}
}

function subUrl(){
	var href = window.location.href;
	var protocol = window.location.protocol;
	var urls =href.split("/");
	var path = protocol+"//"+urls[2]+"/"+urls[3];
	return path;
}

//禁用数字类型的输入框的键盘
$('input[type="num"]').keydown(function(event) {
	var keyCode = event.which;
	// 8是删除键 46是小数点 48-57是0-9
	if ((keyCode >= 48 && keyCode <= 57) || keyCode == 8) {
		return true;
	} else {
		return false;
	}
}).focus(function() {
	this.style.imeMode = 'disabled';
});

// 初始化金额
initMoney();

// 将所有金额类型的输入框的默认值进行转换
function initMoney() {
	$('input[type="money"]').each(function(i) {
		$m = Convert($(this).val());
		$(this).val($m);
	});
	$('input[type="money"]').keypress(function(event) {
		var keyCode = event.which;
		// 8是删除键 46是小数点 48-57是0-9
		if (keyCode == 46 || (keyCode >= 48 && keyCode <= 57) || keyCode == 8) {
			if ($(this).val().indexOf(".") > 0 && keyCode == 46) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}).focus(function() {
		this.style.imeMode = 'disabled';
	});
	$('input[type="money"]').focus(function() {
		var $a = $(this).val().replace(/,|\s/g, '');
		$(this).val($a);
	});
	$('input[type="money"]').blur(function() {
		var $b = Convert($(this).val());
		$(this).val($b);
	});
}
// 禁用金额类型的输入框的键盘
$('input[type="money"]').keypress(function(event) {
	var keyCode = event.which;
	// 8是删除键 46是小数点 48-57是0-9
	if (keyCode == 46 || (keyCode >= 48 && keyCode <= 57) || keyCode == 8) {
		if ($(this).val().indexOf(".") > 0 && keyCode == 46) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}).focus(function() {
	this.style.imeMode = 'disabled';
});

// 金额类型的输入框焦点获取事件
$('input[type="money"]').focus(function() {
	var $a = $(this).val().replace(/,|\s/g, '');
	$(this).val($a);
});

// 金额类型的输入框移开事件
$('input[type="money"]').blur(function() {
	var $b = Convert($(this).val());
	$(this).val($b);
});

// 去除全部金额 样式
function unConvertAll() {
	$('input[type="money"]').each(function(i) {
		var $c = unConvert($(this).val());
		$(this).val($c);
	});
}

// 去除金额 样式
function unConvert(money) {
	money = money.replace(/,|\s/g, '');
	return money;
}

// 金额转换
function Convert(money) {
	var s = money; // 获取小数型数据
	s += "";
	// 如果没有小数点，在后面补个小数点和00
	if (s.indexOf(".") == -1) {
		s += ".00";
	}
	// 如果首位是小数点，则在小数点前补零
	if (s.indexOf(".") == 0) {
		s = "0" + s;
	}
	// 如果最末尾是小数点，则在小数点后补00
	if (s.indexOf(".") == (s.length - 1)) {
		s = s + "00";
	}
	// 正则判断
	if (/\.\d$/.test(s)) {
		s += "0";
	}
	// 符合条件则进行替换
	while (/\d{4}(\.|,)/.test(s)) {
		// 每隔3位添加一个逗号
		s = s.replace(/(\d)(\d{3}(\.|,))/, "$1,$2");
	}
	return s;
}

// =====================jquery validate=====================
// 邮政编码验证
jQuery.validator.addMethod("isZipCode", function(value, element) {
	var tel = /^[0-9]{6}$/;
	return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");

// 数字和字母
jQuery.validator.addMethod("numlett", function(value, element) {
	var tel = /^[A-Za-z0-9]+$/;
	return this.optional(element) || (tel.test(value));
}, "请输入数字、字母或数字+字母的组合");

// 手机号码
jQuery.validator.addMethod("mobilePhone", function(value, element) {
	var tel = /^[1][358][0-9]{9}$/;
	return this.optional(element) || (tel.test(value));
}, "请输入有效的手机号码");

// 金额
jQuery.validator.addMethod("money", function(value, element) {
	var tel = /^((\d{1,3}(,\d{3})+?|\d+)(\.\d{2})?|(\.\d{2}))$/;
	return this.optional(element) || (tel.test(value));
}, "请输入有效的金额");


//日期格式化 yyyy-MM-dd
function changeDateToString(now) {
	var dateTime = "";
	if(now != ""){
		var year = now.year + 1900;
	    var month =now.month + 1;
	    var day = now.date;
	    if (month < 10) {
	        month = "0" + month;
	    }
	    if (day < 10) {
	        day = "0" + day;
	    }
	    dateTime = year+"-"+month+"-"+day;
	}
    return dateTime;
}

//日期格式化 yyyy-MM-dd hh:mm:ss
function changeDateTimeToString(now) {
	var dateTime = "";
	if(now != "" && now != undefined){
		var year = now.year + 1900;
	    var month =now.month + 1;
	    var day = now.date;
	    var hour = now.hours;
	    var minute =now.minutes;
	    var second =now.seconds;
	    if (month < 10) {
	        month = "0" + month;
	    }
	    if (day < 10) {
	        day = "0" + day;
	    }
	    if (hour < 10) {
	    	hour = "0" + hour;
	    }
	    if (minute < 10) {
	    	minute = "0" + minute;
	    }
	    if (second < 10) {
	    	second = "0" + second;
	    }
	    dateTime = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
	}

    return dateTime;
}


//表单数据转成json格式
function formToJson(formObj) {
	var o = {};
	var a = formObj.serializeArray();
	$.each(a, function() {
		if (this.value) {
			if (o[this.name]) {
				if (!o[this.name].push) {
					o[this.name] = [ o[this.name] ];
				}
				o[this.name].push(this.value || null);
			} else {
				if ($("[name='" + this.name + "']:checkbox", formObj).length) {
					o[this.name] = [ this.value ];
				} else {
					o[this.name] = this.value || null;
				}
			}
		}
	});
	return JSON.stringify(o);
}

const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
	'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='

// 加密
function encrypt(txt) {
	const encryptor = new JSEncrypt()
	encryptor.setPublicKey(publicKey) // 设置公钥
	return encryptor.encrypt(txt) // 对数据进行加密
}
